package com.anlogic.sdk.ui.tests.utils;

import java.util.regex.Pattern;

import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.waits.Conditions;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
import org.eclipse.swtbot.swt.finder.waits.ICondition;

import com.anlogic.sdk.ui.tests.utils.BspAppTestUtils.ConsoleTextMatches;

public class SF1Utils {
  public static void sf1(SWTWorkbenchBot bot,String projectName,String download,String lib,boolean usedsp,String newlib,String Optimization) throws InterruptedException {
	  bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"),60*1000);
		
		 bot.waitUntil(new DefaultCondition() {
	            @Override
	            public boolean test() throws Exception {
	                // 检查菜单是否可用
	                return bot.menu("File").menu("New").menu("Project...").isEnabled();
	            }

	            @Override
	            public String getFailureMessage() {
	                return "Menu 'Platform Project' is not available or not enabled.";
	            }
	        },60*1000);
		bot.menu("File").menu("New").menu("Project...").click();
		
		
		bot.tree().getTreeItem("C/C++").select();
		bot.tree().getTreeItem("C/C++").doubleClick();
		bot.tree().getTreeItem("C/C++").getNode("C/C++ Project").select();
		bot.tree().getTreeItem("C/C++").getNode("C/C++ Project").doubleClick();
		bot.table().select(0);
		bot.table().getTableItem(0).doubleClick();
		bot.textWithLabel("&Project name:").setText("sf1");
		bot.tree().getTreeItem("Executable").getNode("RISCV C Project (SF1)").select();
		bot.button("Next >").click();
		bot.comboBox(0).setSelection("sf1_eval");
		bot.comboBox(1).setSelection("N309 (ARCH=rv32imac,ABI=ilp32)");
		bot.comboBox(2).setSelection(download);
		bot.button("Next >").click();
		bot.comboBox(0).setSelection(projectName);
		bot.comboBox(1).setSelection(lib);
		if (!usedsp) {
			bot.checkBox(0).click();
		}
		
		bot.comboBox(2).setSelection(newlib);
		bot.comboBox(3).setSelection(Optimization);
		bot.button("Next >").click();
		bot.button("Finish").click();
		
		bot.viewByTitle("Project Explorer").show();
		bot.viewByTitle("Project Explorer").bot().tree().select("sf1").contextMenu("Build Project").click();
		SWTBotView consoleView = bot.viewByPartName("Console");
		consoleView.setFocus();
		if (consoleView.isActive()) {
			System.out.println(111);
			Pattern p = Pattern.compile(".*Build Finished. 0 errors.*", Pattern.DOTALL);
			
			bot.waitUntil(consoleTextMatches(consoleView, p),25* 1000);
		}

  }
  
  protected static ICondition consoleTextMatches(SWTBotView consoleView, Pattern pattern) {
		return new ConsoleTextMatches(consoleView, pattern);
	}

	static class ConsoleTextMatches implements ICondition {
		private final SWTBotView view;
		private Pattern pattern;

		public ConsoleTextMatches(SWTBotView view, Pattern pattern) {
			this.view = view;
			this.pattern = pattern;
		}

		@Override
		public boolean test() {
			if (view.isActive()) {
				String output = view.bot().styledText().getText();
				java.util.regex.Matcher m = pattern.matcher(output);
				return m.matches();
			}
			return false;
		}

		@Override
		public void init(SWTBot bot) {
		}

		@Override
		public String getFailureMessage() {
			return null;
		}
	}

}
